20225421 HW5 Dahee Kwon

1.1 Initials

lf_img = imread('./chessboard_lightfield.png');
imshow(lf_img);
lightfield = zeros(16,16,400,700,3);
 
for i = 1:16
for j = 1:16
for c = 1:3
lightfield(i,j,:,:,c) = lf_img(i:16:6400,j:16:11200,c);
end
end
end
size(lightfield)
ans = 1×5
16 16 400 700 3

1.2 Sub-aperture views

n = 0;
t = tiledlayout(16, 16, 'TileSpacing', 'none', 'Padding', 'compact');
 
for i = 1:16
for j = 1:16
n = n + 1;
nexttile;
img = uint8(squeeze(lightfield(i,j,:,:,:)));
imshow(img);
end
end

1.3 Refocusing and focal-stack generation

imgs = 0;
for i = 1:16
for j = 1:16
img = squeeze(lightfield(i,j,:,:,:));
imgs = imgs + img;
end
end
 
t = tiledlayout(1,1, 'TileSpacing', 'none', 'Padding', 'compact');
imshow(uint8(imgs/(16*16)))
lensletSize = 16;
maxUV = (lensletSize-1)/2;
u = (1:lensletSize) -1 -maxUV;
v = (1:lensletSize) -1 -maxUV;
S = 1:400;
T = 1:700;
d_list = {0.25, 0, -0.25, -0.5, -0.75, -1, -1.2, -1.5};
fstack = cell(8,1);
 
for d = 1:8
imgs = 0;
for i = 1:16
for j = 1:16
img = squeeze(lightfield(i,j,:,:,:));
s = round(S-d_list{d}*u(i)); s(s>400) = 400; s(s<1) = 1;
t = round(T+d_list{d}*v(j)); t(t>700) = 700; t(t<1) = 1;
img = img(s,t,:);
imgs = imgs + img/(16*16);
end
end
fstack{d} = uint8(imgs);
end
t = tiledlayout(2,4, 'TileSpacing', 'none', 'Padding', 'compact');
for i = 1:8
nexttile;
imshow(fstack{i});
title(d_list{i});
end

1.3 All-focus image and depth from defocus

sigma1 = 2;
sigma2 = 0.5;
 
fstack_lum = cell(8,1);
fstack_low = cell(8,1);
fstack_high = cell(8,1);
wsharp = cell(8,1);
%1 Get luminance channel
for i = 1:8
tmp = rgb2xyz(fstack{i});
fstack_lum{i} = tmp(:,:,2);
end
%2 Create low frequency component
for i = 1:8
tmp = im2double(imgaussfilt(fstack_lum{i},sigma1));
fstack_low{i} = tmp;
end
%3 Compute high frequency component
for i=1:8
fstack_high{i} = fstack_lum{i} - fstack_low{i};
end
%4 Compute the (sharpness) weight
for i = 1:8
tmp = im2double(imgaussfilt((fstack_high{i}).^2,sigma2));
wsharp{i} = tmp;
end
t = tiledlayout(1,1, 'TileSpacing', 'none', 'Padding', 'compact');
nom = 0; denom = 0;
for i = 1:8
nom = nom + wsharp{i}.*im2double(fstack{i});
denom = denom + wsharp{i};
end
focused_img = nom./denom;
 
imshow(focused_img)
nom = 0; denom = 0;
for i = 1:8
nom = nom + wsharp{i}.*(d_list{i}+1.5);
denom = denom + wsharp{i};
end
depthmap = nom./denom;
imshow(depthmap)

Bells & Whistles

nom = 0; denom = 0;
for i = 1:8
nom = nom + wsharp{i}.*(d_list{i}+1.5);
denom = denom + wsharp{i};
end
depthmap = nom./denom;
imshow((depthmap-min(depthmap,[],'all'))/(max(depthmap,[],'all')-min(depthmap,[],'all')));